export default class player extends Laya.Script {
    // 更多参数说明请访问: https://ldc2.layabox.com/doc/?nav=zh-as-2-4-0
        /** @prop {name:bullte, tips:"布尔类型示例", type:Prefab, default:null}*/
        bullte=null;
    rig=null;
    force=1;
    PlayerMaxX=1849;
    PlayerMinX=71;
    owner=this.owner as Laya.Sprite
    isPlay=false;
    spawnBullteArr=[];
    constructor() { super(); }
    onAwake(){
        Laya.stage.on("gamerover",this,this.gamerOver)
        Laya.stage.on("victory",this,this.gamerOver)
        Laya.stage.on("start",this,this.start)
        this.rig=this.owner.getComponent(Laya.RigidBody)
        Laya.stage.on(Laya.Event.MOUSE_DOWN,this,this.mouseDown);
        Laya.stage.on(Laya.Event.MOUSE_UP,this,this.mouseUp);
        Laya.timer.loop(200,this,this.Fire)
    }
    mouseDown(){
        if(!this.isPlay)return;
        var mouseX=Laya.stage.mouseX;
        if(mouseX<Laya.stage.width/2){
            this.force=-1;
            Laya.stage.event("zuo");
            }
            else{
            this.force=1;
            Laya.stage.event("you");
            }
            this.rig.linearVelocity={x:this.force*20,y:0}
    }
    mouseUp(){
        Laya.stage.event("qianjing")
        this.rig.linearVelocity={x:0,y:0}
    }
    onUpdate(){
        if(this.owner.x<this.PlayerMinX){
            this.owner.x=this.PlayerMinX;
            }
            if(this.owner.x>this.PlayerMaxX){
            this.owner.x=this.PlayerMaxX;
    }
}
    Fire(){
        if(!this.isPlay)return;
        var arrX=[this.owner.x-10,this.owner.x+10]
        var y=this.owner.y;
        var x=arrX[this.getRandom(0,arrX.length-1)];
        var bullte=this.bullte.create();
        Laya.stage.addChild(bullte)
        bullte.pos(x,y);
        this.spawnBullteArr.push(bullte);
    }
    onTriggerEnter(other){
        if(other.label=="AI"){
            Laya.stage.event("gamerover")

        }
    }
    start(){
        this.isPlay=true;
        this.owner.pos(960,1006)
    }
    gamerOver(){
        this.isPlay=false;
        this.spawnBullteArr.forEach(element => {
        element.removeSelf();
        });
        }

    getRandom(min,max){
        var value=Math.random()*(max-min);
        value=Math.round(value);
        return min+value;
        }
}